// 存储角色对应的权限信息
const roleToRoute = {
  // 用户
  coustomer: [
    {
      name: 'Prodouct',
    },
    {
      name: 'ProdouctList',
    },
    {
      name: 'ProdouctAdd',
    },
    {
      name: 'ProdouctEdit',
    },
  ],
  admin: [
    {
      name: 'ProdouctEdit',
    },
    {
      name: 'Prodouct',
    },
    {
      name: 'ProdouctList',
    },
    {
      name: 'ProdouctAdd',
    },
    {
      name: 'Category',
    },
  ],
};

// 过滤没有权限的路由
export default function getMenu(role, routs) {
  // 有权限允许的用户名
  const allowRoutesName = roleToRoute[role].map((item) => item.name);
  // 过滤没有权限的
  const ruslutRoutes = routs.filter((r) => {
    const obj = r;
    if (allowRoutesName.indexOf(r.name) !== -1) {
      // 遍历子路由判断权限
      const { children } = obj;
      obj.children = children.filter((c) => allowRoutesName.indexOf(c.name) !== -1);
      return true;
    }
    return false;
  });
  return ruslutRoutes;
}
